#define _CRT_SECURE_NO_WARNINGS
//#include <iostream>
//typedef long long LL;
//using namespace std;
//
//LL ret;
//void  dfs(LL x, LL y)
//{
//	if (x == 0 || y == 0) return;
//
//	LL max = x, min = y;
//	if (x < y) max = y, min = x;
//	ret += min * 4;
//	dfs(min, max - min);
//}
//int main()
//{
//	LL x, y; scanf("%lld %lld", &x, &y);
//	dfs(x, y);
//	printf("%lld", ret);
//
//	return 0;
//}
#include <iostream>
typedef long long LL;
using namespace std;

LL ret;
int main()
{
	LL x, y; scanf("%lld %lld", &x, &y);


	while (x && y)
	{
		LL cnt = x / y;
		ret += cnt * y * 4;
		x %= y;
		swap(x, y);
	}
	printf("%lld", ret);

	return 0;
}